var loader;
var upload = false;
var mapInit = false;
/**
 * ajax从服务器得到的商户信息
 */
var busInfos = {};

$(function() {
    
    selectDataObtain("type");
    
    $(".input_view, #type").change(function() {
	$("#button_content").css("display", "block");
    });
    
    // 获取数据进行填充
    obtainInfos();
    
    initDialog();
});

function initDialog() {
    // 更改LOGO弹出框
    $("#logo_select_content").dialog({
	autoOpen : false,
	height : 500,
	width : 530,
	modal : true,
	buttons : {
	    "确定" : logo_select_dialog_ensure,
	    "取消" : closeThisDialog
	},
	show : {
	    effect : "fade",
	    duration : 200
	},
	hide : {
	    effect : "fade",
	    duration : 300
	},
	open : function() {
	    $("#bigPrevView").before($("#logo_select")[0]);
	    $("#logo_select").css("display", "inline");
	},
	beforeClose : function() {
	    $("#infos_form").append($("#logo_select")[0]);
	    $("#logo_select").css("display", "none");
	},
	close : function() {
	    if (!$("#logo_select").val()) {
		$("#bigPrevView, #middlePrevView, #smallPrevView, #bus_logo").attr("src", busInfos.logo);
	    }
	}
    });
    
    // 编辑信息失败Dialog
    $("#fail_dialog").dialog({
	autoOpen : false,
	modal : true,
	buttons : {
	    "确定" : closeThisDialog
	},
	show : {
	    effect : "shake",
	    duration : 300
	},
	hide : {
	    effect : "fade",
	    duration : 300
	}
    });
    
    // 地图Dialog
    $("#map_dialog").dialog({
	autoOpen : false,
	modle : true,
	width : "90%",
	height : window.innerHeight - 10,
	buttons : {
	    "确定" : closeThisDialog,
	},
	close : function() {
	  if (marker.getPosition().lat != busInfos.lat || marker.getPosition().lng != busInfos.lon) {
	      $("#button_content").css({"display": "block"});
	      $("#lat").val(marker.getPosition().lat);
	      $("#lon").val(marker.getPosition().lng);
	  }  
	},
	show : {
	    effect : "fade",
	    duration : 200
	},
	hide : {
	    effect : "fade",
	    duration : 300
	}
    });
    
}

function obtainInfos() {
    $.post("/QuickLifeServer/GetBusInfosAction", 
	    {"actionflag": "busdetail", "rqinfos": $("#userid").val()},
	    function(rtinfos) {
		rt = JSON.parse(rtinfos);
		if (rt.result.toUpperCase() == "SUCCESS") {
		    busInfos = rt.rtinfos[0];
		    setInfo();
		} else {
		    $("#fail_info").text(rt.rtinfos);
		    $("#fail_dialog").dialog("open");
		}
    });
}

function setInfo() {
    $("#name").val(busInfos.name);
    $("#address").val(busInfos.address);
    $("#score").val(busInfos.score);
    $("#type option#" + busInfos.typename).attr("selected", "selected");
    $("#phone").val(busInfos.phone);
    $("#bus_logo, #bigPrevView, #middlePrevView, #smallPrevView").attr("src", busInfos.logo);
    $("#lat").val(busInfos.lat);
    $("#lon").val(busInfos.lon);
    // 父控件的也设置一下，正常来说，父控件都是跟这里的是一样的，会不一样是因为修改过资料
    $(window.parent.document).find("#bus_logo").attr("src", busInfos.logo);
    $(window.parent.document).find("#name").text(busInfos.name);
    var point = new BMap.Point(busInfos.lon, busInfos.lat);
    marker.setPosition(point);
    $("#button_content").css("display", "none");
}

function location_address() {
    if (!mapInit) {
	// 地图初始化
	initMap("map", "address");
	mapInit = true;
    }
    $("#map_dialog").dialog("open");
}

function change_logo() {
    $("#logo_select_content").dialog("open");
}

function logo_select_dialog_ensure() {
    if ($("#logo_select").val()) {
	show_img($("#bus_logo"), $("#logo_select").prop("files")[0]);
    }
    $("#logo_select_content").dialog("close");
}

function findAddress() {
	addressSet($("#address_find").val());
}

/**
 * 把页面数据打包成表单进行上传
 */
function uploadForm() {
    loader = new ajaxLoader($("body")[0], {classOveride : "blue-loader"});
    Ext.Ajax.request({
	url: "/QuickLifeServer/BussInfoManageAction?actionflag=mdfbuss",
	isUpload: true,
	form: "infos_form",
	success: changeFinish
    });
}

function changeFinish(result) {
    if (loader) {
	loader.remove();
    }
    var rt = JSON.parse(result.responseText);
    if (rt.result.toUpperCase() == "SUCCESS") {
	$("#button_content").css("display", "none");
	// 修改成功重新设置值 
	obtainInfos();
    } else {
	$("#fail_info").text("修改失败，请重试");
	$("#fail_dialog").dialog("open");
    }
}

/**
 * 图片上传
 */
function uploadLogo() {
    // 当前是否上传中
    if (upload) {
	return;
    }
    upload = true;
    // 得到选取的文件
    var img = $("#logo_select").prop("files")[0];
    if (img) {
	if (img.type.match("image.*")) {
	    loader = new ajaxLoader($("#logo_select_content")[0], {
		classOveride : "blue-loader"
	    });
	    var formData = new FormData();
	    formData.append("logo", img);
	    // ajax上传图片
	    ajaxUpload(formData, "/QuickLifeServer/ChangeLogoAction", function(
		    rtinfos) {
		var infos = JSON.parse(rtinfos);
		if (infos.result) {
		    $("#bus_logo").attr("src", rtinfos.url);
		} else {
		    $("#fail_info").text("LOGO上传失败");
		    $("#fail_dialog").dialog("open");
		}
		upload = false;
	    });
	}
    }
}

function prev_logo() {
    // 遍历选择文件
    var file = $("#logo_select").prop("files")[0];
    // 排除非图片选择
    if (file.type.match("image/*")) {
	show_img($("#bigPrevView, #middlePrevView, #smallPrevView"), file);
    } else {
	$("#logo_select").val(null);
    }
}